package com.yjc.drygoods.product.service.impl;

import com.yjc.common.utils.PageUtils;
import com.yjc.drygoods.product.dao.AttrAttrgroupRelationDao;
import com.yjc.drygoods.product.dao.AttrGroupDao;
import com.yjc.drygoods.product.entity.AttrAttrgroupRelationEntity;
import com.yjc.drygoods.product.entity.AttrEntity;
import com.yjc.drygoods.product.entity.AttrGroupEntity;
import com.yjc.drygoods.product.service.AttrGroupService;
import com.yjc.drygoods.product.service.AttrService;
import com.yjc.drygoods.product.vo.AttrGroupWithAttrsVo;
import com.yjc.drygoods.product.vo.SkuItemAttrGroupVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjc.common.utils.Query;

import org.springframework.util.StringUtils;

@Slf4j
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );


        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatelogId(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            log.info("gg");
            wrapper.and(i -> i.eq("attr_group_id", key).or().like("attr_group_name", key));
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }

    }

    @Override
    public void deleteRelation(AttrAttrgroupRelationEntity[] entity) {
        attrAttrgroupRelationDao.deleteBatchRelation(entity);

    }

    @Override
    public List<AttrGroupWithAttrsVo> getAttrAttrGroup(Long catelogId) {

        List<AttrGroupEntity> attrGroupEntities
                = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        if (attrGroupEntities != null && attrGroupEntities.size() > 0) {
            List<AttrGroupWithAttrsVo> collects = attrGroupEntities.stream().map(item -> {
                AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
                BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
                List<AttrEntity> relationAttrs = attrService.getRelationAttr(item.getAttrGroupId());
                attrGroupWithAttrsVo.setAttrs(relationAttrs);
                return attrGroupWithAttrsVo;
            }).collect(Collectors.toList());
            return collects;
        }
        return null;

    }

    @Override
    public List<SkuItemAttrGroupVo> getAttrGroupWithAttrsBySpuIdAndCatelogId(Long spuId, Long catalogId) {
        return this.baseMapper.selectAttrGroupWithAttrsBySpuIdAndCatelogId(spuId, catalogId);
    }


}